package com.young.nyd.service.impl;

import com.young.common.core.dal.service.IDataAccessService;
import com.young.common.util.CommonUtil;
import com.young.common.util.Page;
import com.young.nyd.model.UviAgriculturallandinformation;
import com.young.nyd.model.UviCultivatechange;
import com.young.nyd.service.IUviAgriculturallandinformationService;
import com.young.nyd.service.IUviCultivatechangeService;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 耕地分类变更表服务实现
 * Created by hu on 2018-11-4.
 */
@Service("uviCultivatechangeService")
public class UviCultivatechangeServiceImpl implements IUviCultivatechangeService {

    @Resource(name = "dataAccessService")
    IDataAccessService dataAccessService;//数据层服务

    @Resource(name = "uviAgriculturallandinformationService")
    IUviAgriculturallandinformationService uviAgriculturallandinformationService;

    private static final Logger logger = LoggerFactory.getLogger(UviCultivatechangeServiceImpl.class);

    private static final String MAPPER = "com.young.nyd.mapper.uviCultivatechange";//mapper的namespace

    //获取数据列表
    @Override
    public List<UviCultivatechange> getList(UviCultivatechange obj) {
        return dataAccessService.getList(MAPPER + ".getList", obj);
    }

    //获取数据列表(分页)
    @Override
    public List<UviCultivatechange> getPage(UviCultivatechange obj, Page page) {
        return dataAccessService.getList(MAPPER + ".getList", obj, page);
    }

    //查询单条
    @Override
    public UviCultivatechange get(String id) {
        return dataAccessService.getObject(MAPPER + ".get", id);
    }

    //统计数量
    @Override
    public int count(UviCultivatechange obj) {
        return dataAccessService.getObject(MAPPER + ".count", obj);
    }

    //修改
    @Override
    public int update(UviCultivatechange obj) {
        return dataAccessService.update(MAPPER + ".update", obj);
    }

    //批量删除
    @Override
    public int delete(String[] idArr) {
        int i = 0;
        for (String id : idArr) {
            i += this.delete(id);
        }
        return i;
    }

    //删除单条
    @Override
    public int delete(String id) {
        return dataAccessService.delete(MAPPER + ".delete", id);
    }

    //插入
    @Override
    public int insert(UviCultivatechange obj) {
        obj.setId(CommonUtil.getUUID());
        return dataAccessService.insert(MAPPER + ".insert", obj);
    }

    //批量插入
    @Override
    public int batchInsert(List<UviCultivatechange> list) {
        int i = 0;
        for (UviCultivatechange item : list) {
            i += this.insert(item);
        }
        return i;
    }

    /**
     * 分类变更
     *
     * @param obj
     * @return
     */
    @Override
    public int cultivateChange(UviCultivatechange obj) throws Exception {
        //农用地信息,未变更前的
        UviAgriculturallandinformation oldLand = uviAgriculturallandinformationService.get(obj.getOrganizationcode());
        //获得tilltype(优先:1；安全:2；严格:3)
        String oldTilltype = String.valueOf(oldLand.getTilltype());
        if(!oldTilltype.equals("")) {
            if (oldTilltype.equals("1")) {
                oldTilltype = "优先保护类耕地";
            } else if (oldTilltype.equals("2")) {
                oldTilltype = "安全利用类耕地";
            } else {
                oldTilltype = "严格管控类耕地";
            }
        }

        //1.更新耕地类型
        UviAgriculturallandinformation land = new UviAgriculturallandinformation();
        land.setId(obj.getOrganizationcode());
        //获取变更后的耕地类型
        String newafter = obj.getTypenameafter();
        //将获取的变更后类型放入农用地信息表
        land.setTilltype(Integer.parseInt(newafter));
        if(!newafter.equals("")){
            if(newafter.equals("1")){
                newafter = "优先保护类耕地";
            }else if (newafter.equals("2")) {
                newafter = "安全利用类耕地";
            } else {
                newafter = "严格管控类耕地";
            }
        }

        //放入变更后耕地类型
        //land.setCultivatedland(newafter);
        uviAgriculturallandinformationService.update(land);
        System.out.println("=================================="+uviAgriculturallandinformationService.update(land));

        //2.记录变更日志
        //变更前耕地类型(tilltype：优先:1；安全:2；严格:3)
        obj.setTypenamebefore(oldTilltype);
        obj.setTypenameafter(newafter);
        //obj.setTypenamebefore(oldLand.getCultivatedland());//变更前用户手动填写的耕地类型
        obj.setOrganizationcode(oldLand.getOrganizationcode());
        obj.setCultivatedland(oldLand.getCultivatedland());//耕地类型
        obj.setRegions(oldLand.getRegions());//行政区划
        obj.setLandname(oldLand.getLandname());//地块名称
        Date date = new Date();//获取当前时间
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dd = sdf.format(date);
        obj.setChangetime(sdf.parse(dd));//日期
        obj.setTypename(oldLand.getTypename());//类别名称
        obj.setLongitude(oldLand.getLongitude());//经度
        obj.setDimension(oldLand.getDimension());//纬度
        obj.setOwnership(oldLand.getOwnership());//权属
        // 其他需要记录的字段

        return this.insert(obj);//插入变更日志
    }

    /**
     * 查询最大序号
     * @param obj
     * @return
     * @throws Exception
     */
    @Override
    public String maxSerialnumber(UviCultivatechange obj) throws Exception {
        String max = dataAccessService.getObject(MAPPER + ".maxSerialnumber",obj);
        return max;
    }


}